package com.achen.workQueue;

import com.achen.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author achen
 * @create 2020-05-10 1:46
 */
public class Consumer1 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtil.getConnection();

        //获取连接中通道
        Channel channel = connection.createChannel();
        //通道绑定消息队列
        channel.queueDeclare("work", true, false, false, null);
        channel.basicQos(1);
        //消费消息
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者1 ===> " + new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);

            }
        });
    }
}
